/*
 * 类名：UserServiceImpl
 * 
 * 版本信息：0.0.1
 *
 * 作成日期：2018/12/10
 * 
 * 作成者：luyou
 */
package com.dd.census.service.impl;

import java.util.HashMap;
import java.util.Map;

import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dd.census.entity.SysUser;
import com.dd.census.entity.SysUserRole;
import com.dd.census.mapper.SysUserMapper;
import com.dd.census.mapper.SysUserRoleMapper;
import com.dd.census.service.SysUserService;
import com.dd.census.tools.SysException;
import com.dd.census.tools.TokenHelper;

@Service
public class SysUserServiceImpl implements SysUserService {

	@Autowired
	private SysUserMapper userMapper;

	@Autowired
	private SysUserRoleMapper sysUserRoleMapper;

	public SysUser selectById(Integer id) {
		return userMapper.selectById(id);
	}

	public Map<String, Object> selectLogin(SysUser user) {
		Map<String, Object> map = new HashMap<String, Object>();

		SysUser userNew = userMapper.selectLogin(user);
		if (null == userNew) {
			throw new SysException(HttpStatus.NOT_FOUND);
		}
		if (!BCrypt.checkpw(user.getPassword(), userNew.getPassword())) {
			throw new SysException(HttpStatus.NOT_FOUND);
		}
		map.put("user", userNew);
		// 更新Token信息
		String token = TokenHelper.generateToken(userNew);
		map.put("token", token);

		return map;
	}

	public Map<String, Object> selectWxLogin(SysUser user) {
		Map<String, Object> map = new HashMap<String, Object>();

		SysUser userNew = userMapper.selectLogin(user);

		if (user.getPassword() != null) {
			if (!BCrypt.checkpw(user.getPassword(), userNew.getPassword())) {
				throw new SysException(HttpStatus.NOT_FOUND);
				//map.put("PawStatus", false);
			}
		}
		map.put("user", userNew);
		return map;
	}

	public int updatePassword(SysUser user) {
		user.setPassword(BCrypt.hashpw(user.getPassword(), BCrypt.gensalt()));

		return userMapper.updatePassword(user);
	}

	public Map<String, Object> selectPage(SysUser user) {
		Map<String, Object> map = new HashMap<String, Object>();
		int total = userMapper.selectCount(user);
		map.put("pageCount", Math.ceil((double) total / user.getPageSize()));// 总页数
		map.put("total", total);// 总条数
		map.put("index", user.getPageIndex());// 当前页
		if (total > 0) {
			user.setPageIndex(user.getPageSize() * (user.getPageIndex() - 1));
			map.put("items", userMapper.selectPage(user));
		}
		return map;
	}
	
	public SysUser selectAll(SysUser user) {
		return userMapper.selectAll(user);
	}

	@Transactional
	public int update(SysUser user) {
		int count = this.userMapper.update(user);

		if (user.getStatus() != 1) {
			// 先删除此用户的所有角色
			SysUserRole srm = new SysUserRole();
			srm.setUserId(user.getId());
			sysUserRoleMapper.deleteRoleId(srm);

			// 同时添加新的角色
			SysUserRole userRole = new SysUserRole();
			for (int i = 0; i < user.getRole().size(); i++) {
				Integer roleId = user.getRole().get(i);
				userRole = new SysUserRole();
				userRole.setUserId(user.getId());
				userRole.setRoleId(roleId);
				sysUserRoleMapper.insert(userRole);
			}
		}

		return count;
	}
	
	public int wxUpdate(SysUser user) {
		return userMapper.update(user);
	}

	public int updatePass(SysUser user) {
		int count = this.userMapper.update(user);

		return count;
	}

	@Transactional
	public int insert(SysUser user) {
		userMapper.insert(user);

		// 同时添加角色
		Integer id = user.getId();
		SysUserRole srm = new SysUserRole();
		for (int i = 0; i < user.getRole().size(); i++) {
			Integer roleId = user.getRole().get(i);
			srm = new SysUserRole();
			srm.setUserId(id);
			srm.setRoleId(roleId);
			sysUserRoleMapper.insert(srm);
		}
		return user.getId();
	}

	public SysUser getSysUserById(Integer id) {
		return userMapper.getSysUserById(id);
	}
}